<%args>
	$dbh
	$auth
	$id => undef
</%args>
<%init>
#use Data::Dumper;
use POSIX;
use locale;
#setlocale(&LC_COLLATE, 'ru_RU.KOI8-R');
use utf8;

if($auth->{gid} ne '1' && $auth->{gid} ne '2') {	# admin или manager
	$r->status_line('');
	$m->clear_buffer;
	$m->abort(404);
}
my $self = 'tarifs.html';

if($ARGS{cancel}) {
	if($id || $ARGS{add_new}) {
		$r->status_line('');
		$m->clear_buffer;
		$m->redirect($self);
	} else {
		$r->status_line('');
		$m->clear_buffer;
		$m->abort(200);
	}
}


#print STDERR " id=$id\n";
#print STDERR Dumper %ARGS;


map { $_ =~ s/\D//g; } ( $id );

my %Err;



# Текущая дата
#my $Today = $m->comp("lib.msn:now", nocache=>1);


if($ARGS{submit}) {

	my ($sdate,$t0,$t1,$t2,$k);

	if($ARGS{sdate} =~ /(\d{1,2})[\-\.](\d{1,2})[\-\.](\d{4})/) {
		$sdate = sprintf("%d-%02d-%02d",$3,$2,$1);
	} else {
		$Err{sdate}++;
	}
	map { $_ =~ s/\s//g; } ( $ARGS{t0}, $ARGS{t1}, $ARGS{t2}, $ARGS{k} );

	if($ARGS{t0} =~ /(\d+)[\.\,]*(\d*)/) {
		$t0 = $1.($2 ? ".$2":"");
	} else  {
		$Err{t0}++;
	}
	if($ARGS{t1} =~ /(\d+)[\.\,]*(\d*)/) {
		$t1 = $1.($2 ? ".$2":"");
	} else  {
		$Err{t1}++;
	}
	if($ARGS{t2} =~ /(\d+)[\.\,]*(\d*)/) {
		$t2 = $1.($2 ? ".$2":"");
	} else  {
		$Err{t2}++;
	}
	if($ARGS{k} =~ /(\d+)[\.\,]*(\d*)/) {
		$k = $1.($2 ? ".$2":"");
	} else  {
		$Err{k}++;
	}

	if($id) { 	# Редактируем запись

		if($ARGS{drop}) { # Удаляем запись
			my $del = $dbh->prepare("delete from tariff where id=?");
			$del->execute($id);
#			undef $id;
			$r->status_line('');
			$m->clear_buffer;
			$m->redirect($self);
		} else {
			my $upd = $dbh->prepare("update tariff set auth=$auth->{id},modtime=now(),sdate=?,t0=?,t1=?,t2=?,k=? where id=?");
			unless(%Err) {
				$upd->execute($sdate,$t0,$t1,$t2,$k,$id);
#				undef $id;
				$r->status_line('');
				$m->clear_buffer;
				$m->redirect($self);
			}
		}

	} else { # Новая запись
		my $ins = $dbh->prepare("insert into tariff (auth,sdate,t0,t1,t2,k) values ($auth->{id},?,?,?,?,?)");
		unless(%Err) {
			$ins->execute($sdate,$t0,$t1,$t2,$k);
#			undef $ARGS{add_new};
			$r->status_line('');
			$m->clear_buffer;
			$m->redirect($self);
		}
	}
} else {
}

# Тарифы
my $T = $m->comp("lib.msn:tariff", dbh=>$dbh, nocache=>1);




</%init>
<script src="calendar.js" type="text/javascript"></script>

<pre>
%#<%Dumper $id%>
%#<%Dumper %ARGS%>
</pre>

<form method=POST>
<table border="1">
<tr><td colspan="5" align="right"><nobr>
%if(!$ARGS{add_new} && !$id) {
<input type="submit" name="add_new" value="+" title="добавить новый тариф">
%}
<input type="submit" name="cancel" value="X" title="отменить"></nobr></td></tr>
%if($id || $ARGS{add_new}) {

<tr><td><div style=color:<% $Err{sdate} ? "red":"black"%>>Дата начала:</div></td><td colspan="3"><input type="text" name="sdate" value="<% $ARGS{sdate} || $m->comp("lib.msn:pretty_date",date=>$T->{$id}->{sdate}) || "dd.mm.yyyy"%>" onfocus="this.select();_Calendar.lcs(this)" onclick="event.cancelBubble=true;this.select();_Calendar.lcs(this)"></td></tr>
<tr><td><div style=color:<% $Err{t1} ? "red":"black"%>>Один:</div></td><td colspan="3"><input type="text" name="t0" value="<% $ARGS{t0} || $m->comp("lib.msn:pretty",value=>sprintf("%0.2f",$T->{$id}->{t0})) || ""%>"></td></tr>
<tr><td><div style=color:<% $Err{t1} ? "red":"black"%>>День:</div></td><td colspan="3"><input type="text" name="t1" value="<% $ARGS{t1} || $m->comp("lib.msn:pretty",value=>sprintf("%0.2f",$T->{$id}->{t1})) || ""%>"></td></tr>
<tr><td><div style=color:<% $Err{t2} ? "red":"black"%>>Ночь:</div></td><td colspan="3"><input type="text" name="t2" value="<% $ARGS{t2} || $m->comp("lib.msn:pretty",value=>sprintf("%0.2f",$T->{$id}->{t2})) || ""%>"></td></tr>
<tr><td><div style=color:<% $Err{k} ? "red":"black"%>>Коэффициент:</div></td><td colspan="3"><input type="text" name="k" value="<% $ARGS{k} || $m->comp("lib.msn:pretty",value=>sprintf("%0.2f",$T->{$id}->{k})) || ""%>"></td></tr>

%	if($ARGS{add_new}) {
<tr><td></td><td colspan="3"><input type="submit" name="submit" value="Сохранить"></td></tr>
<input type="hidden" name="add_new" value="1">
%	} else {
<tr><td colspan="4" align="center"><input type="checkbox" name="drop"><i>удалить</i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="submit" name="submit" value="OK"></td></tr>
%	}
<tr><td colspan="4" align="right"><br><br><br></td></tr>
%} else {
<tr><td colspan="4" align="right"></td></tr>
%}
</form>

<tr><th>Дата</th><th>Один</th><th>День</th><th>Ночь</th><th>Коэффициент</th></tr>
%foreach my $tid (sort { $T->{$b}->{sdate} cmp $T->{$a}->{sdate}} keys %{$T}) {
%	next if $tid ne 'C' && $T->{$tid}->{id} ==  $T->{C}->{id};
%	my $color = $tid eq 'C' ? "green" : "black";
<tr>
<td><div style=color:<%$color%>><a href="?id=<% $T->{$tid}->{id}%>"><% $m->comp("lib.msn:pretty_date",date=>$T->{$tid}->{sdate}) %></a></div></td>
<td align="right"><div style=color:<%$color%>><b><% $m->comp("lib.msn:pretty", value=>sprintf("%0.2f",$T->{$tid}->{t0})) %></b></div></td>
<td align="right"><div style=color:<%$color%>><b><% $m->comp("lib.msn:pretty", value=>sprintf("%0.2f",$T->{$tid}->{t1})) %></b></div></td>
<td align="right"><div style=color:<%$color%>><b><% $m->comp("lib.msn:pretty", value=>sprintf("%0.2f",$T->{$tid}->{t2})) %></b></div></td>
<td align="right"><div style=color:<%$color%>><b><% $m->comp("lib.msn:pretty", value=>sprintf("%0.2f",$T->{$tid}->{k})) %></b></div></td>
</tr>

%}
</table>


